import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { AdminService } from './admin.service';

@Component({
    moduleId: module.id,
    selector: 'admin',
    templateUrl: 'admin.component.html',
    styleUrls: ['admin.component.css']
})

export class AdminComponent implements OnInit {
    firstLevelMenu: any[] = [];
    menuList: any[];
    isParent: boolean = false;
    ngOnInit(): void {
        this.getMenu();
    }

    constructor(
        private router: Router,
        private adminService: AdminService
    ) { }


    // 查询菜单列表
    getMenu(): void {
        this.menuList = this.adminService.queryMenu();
        if (this.menuList.length) {
            for (let i = 0; i < this.menuList.length; i++) {
                this.firstLevelMenu.push({
                    FmName: this.menuList[i].FmName
                });
            }
            this.currentUrl(0);
        }
    }

    // 当前路由
    currentUrl(index: number): void {
        if (index < this.menuList.length) {
            const children = this.menuList[index].Children;
            this.firstLevelMenu[index].Children = this.menuList[index].Children;
            this.firstLevelMenu[index].parent = true;
        }
    }

    // 点击显示/隐藏子菜单
    onSelect(index: number) {
        if (this.firstLevelMenu[index].Children) {
            delete this.firstLevelMenu[index].Children;
            delete this.firstLevelMenu[index].parent;
        } else {
            for (let i = 0; i < this.firstLevelMenu.length; i++) {
                if (i === index && this.menuList[index].Children && this.menuList[index].Children.length) {
                    this.firstLevelMenu[index].Children = this.menuList[index].Children;
                    this.firstLevelMenu[index].parent = true;
                } else {
                    delete this.firstLevelMenu[i].Children;
                    delete this.firstLevelMenu[i].parent;
                }
            }
        }
    }

}
